﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MifareClassIC卡读写DEMO</title>

    <script language="javascript">
        var ws;
        var received_msg = "";
        var mytimer;
        var strls = "";
        var errorno = "";
        var readcarnow = false;
		var time1;

        var wsUri = "ws://127.0.0.1:39189";   //端口号必须与RFIDWebServer端口一致


        function beep() {				     //驱动发卡器响声令

            textarea.value = "";		
            WebSocketRun("pcdbeep,30");		
						
        }
        function piccrequest() {
            carduid.value = "";
            // card8h10dz.value = "";
            // card8h10df.value = "";
            textarea.value = "";

            var RequestCardComm = "piccrequest,";
            WebSocketRun(RequestCardComm);
        }		
		
		
		function DispErrInfo(errcode){
            var errstr = "";
            switch (errcode) {
                case "ReturnCode:001":
                    errstr = "，读写扇区数据失败！";
                    break;
                case "ReturnCode:002":
                    errstr = "，读写1、2块数据失败！";
                    break;
                case "ReturnCode:003":
                    errstr = "，读写第2块数据失败！";
                    break;
                case "ReturnCode:008":
                    errstr = "，未寻到卡，请将卡放到发卡器的感应区！";
                    break;
                case "ReturnCode:009":
                    errstr = "，有多张卡在感应区，寻卡过程中防冲突失败，读卡失败！";
                    break;
                case "ReturnCode:010":
                    errstr = "，该卡可能已被休眠，无法选中卡片！";
                    break;
                case "ReturnCode:011":
                    errstr = "，密码装载失败！";
                    break;
                case "ReturnCode:012":
                    errstr = "，卡密码认证失败！";
                    break;
                case "ReturnCode:013":
                    errstr = "，读块操作失败，原因是刷卡太快或本块所对应的区还没通过密码认证！";
                    break;
                case "ReturnCode:014":
                    errstr = "，写块操作失败，原因是刷卡太快或本块所对应的区还没通过密码认证！";
                    break;
                case "ReturnCode:018":
                    errstr = "，写块操作失败！";
                    break;
                case "ReturnCode:021":
                    errstr = "，没有动态库！";
                    break;
                case "ReturnCode:022":
                    errstr = "，动态库或驱动程序异常！";
                    break;
                case "ReturnCode:023":
                    errstr = "，驱动程序错误或发卡器未连接！";
                    break;
                case "ReturnCode:024":
                    errstr = "，操作超时，一般是动态库没有反映！";
                    break;
                case "ReturnCode:025":
                    errstr = "，发送字数不够！";
                    break;
                case "ReturnCode:026":
                    errstr = "，发送的CRC错！";
                    break;
                case "ReturnCode:027":
                    errstr = "，接收的字数不够！";
                    break;
                case "ReturnCode:028":
                    errstr = "，接收的CRC错！";
                    break;
                case "ReturnCode:029":
                    errstr = "，函数输入参数格式错误！";
                    break;
                case "ReturnCode:444":
                    errstr = "，RFIDWebServer系统文件错误！";
                    break;
                default:
                    errstr = "，未知的错误！";
            }
            return errstr;
		}			
		
		
		window.onerror = function (e) {
			clearInterval(time1);
            alert("不好意思，出错了!");
            return true;//屏蔽系统事件
        }

        function timeoutevent() {
            ws.close();
            textarea.value = "websockket返回超时";
			clearInterval(time1);
        }

        //websockket数据处理
        function WebSocketRev(evt) {
            received_msg = evt.data;
            ws.close();
            //在这里解析返回数据
            var strlist = received_msg.split(",");
            if (strlist.length > 1) {
                var dispstr = "";
                switch (strlist[0]) {
                    case "pcdbeep":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + "，已执行蜂鸣响声操作！\r\n";
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        textarea.value = dispstr;
						piccrequest()
                        break;
                    case "piccrequest":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + "，已读取Mifare卡卡号！\r\n";
							carduid.value=strlist[2];							
							// card8h10dz.value=parseInt("0x"+strlist[2]).toString().padStart(10, '0');
							// card8h10df.value=strlist[3];
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        textarea.value = dispstr;
                        break;

                    case "piccread":
                        dispstr = "函数名称:" + strlist[0] + "\r\n";
                        if (strlist[1] == "ReturnCode:000") {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + "，已读取卡内数据！\r\n";
                        } else {
                            dispstr = dispstr + "操作结果:" + strlist[1];
                            dispstr = dispstr + DispErrInfo(strlist[1]);
                        }
                        if (strlist.length > 2) {
                            carduid.value = strlist[2];
                            // HLCode = strlist[2].substring(0, 2) + strlist[2].substring(2, 4) + strlist[2].substring(4, 6) + strlist[2].substring(6, 8);
                            // card8h10dz.value = parseInt("0x" + HLCode).toString().padStart(10, '0');

                            // LHCode = strlist[2].substring(6, 8) + strlist[2].substring(4, 6) + strlist[2].substring(2, 4) + strlist[2].substring(0, 2);
                            // card8h10df.value = parseInt("0x" + LHCode).toString().padStart(10, '0');
                        }
                        if (strlist.length > 3) {
                            blockdata.value = strlist[3].substring(0, 32);
                        }
                        textarea.value = dispstr;
                        break;
                }
            }
        }		
		
        function WebSocketRun(sendinfo) {
            iswsrun = false;

            try {
                if ("WebSocket" in window) {
                    ws = new WebSocket(wsUri);
                }
                else if ("MozWebSocket" in window) {
                    ws = new MozWebSocket(wsUri);
                }
                else {
                    received_msg = "您的浏览器不支持WebSocket，请选用支持WebSocket的浏览器！";
                    return;
                }

                ws.onopen = function (evt) {
                    ws.send(sendinfo);
                    iswsrun = true;

                    mytimer = setTimeout("timeoutevent()", 2000);
                };

                ws.onmessage = WebSocketRev;

                ws.onerror = function (e) {
                        received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
                        window.open("http://www.bossknow.cn:90/RongShiWebReaderTest/RFIDWebServerSetup_Windows.rar", "top");//打开新窗口
						clearInterval(time1);
                        alert("请先下载>安装>运行我们的服务程序，再刷新本页面......");
                };
            }
            catch (ex) {
                    received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
                    window.open("http://www.bossknow.cn:90/RongShiWebReaderTest/RFIDWebServerSetup_Windows.rar", "top");//打开新窗口
					clearInterval(time1);
                    alert("请先下载>安装>运行我们的服务程序，再刷新本页面......");
            }
        }				
	</script>
        
</head>

<body>
<table width="866" height="484"  align="center">
  <tr>
    <th height="79" scope="row">
		<th width="124" height="45" scope="row"><input style="width:120px" name="butt_beep" type="submit" id="butt_beep" onclick="beep()" value="驱动发卡器响声" /></th>
    </th>
    <td>原始16进制卡号:
      <input style="color:red;text-align:center;" name="carduid" type="text" id="carduid" size="8" maxlength="8" />
<!--      ，转8H10D正码:
      <input style="color:red;text-align:center;" name="card8h10dz" type="text" id="card8h10dz" size="10" maxlength="10" />
      ，8H10D反码:
      <input style="color:red;text-align:center;" name="card8h10df" type="text" id="card8h10df" size="10" maxlength="10" /></td> -->
  </tr>

  <tr>
    <th height="100" scope="row"><p>操作提示</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p></th>
    <td><textarea style="color:blue;" name="textarea" id="textarea" cols="100" rows="8" ></textarea></td>
  </tr>


</table>
</body>
</html>
